// pages/punch/punch.js
const utils = require('../../utils/util.js');
const post = utils.post;
import Toast from './../../dist/toast/toast';
let urlPunch = utils.domain + '/activity.php';
// let urlWinxinPay=''
// const imgUrl=
Page({
  /*** 页面的初始数据*/
  data: {
    top: "1207rpx",
    display: "none",
    radio: "1",
    shoppCount: "1",
    uid: "",
    imgUrlOne: "url('https://image.whwangdoudou.cn/extra/activity_btn.png')",
    imgUrlTwo: "",
    imgUrlThree: "",
    imgUrlFour: "",
    balance: "",
    success: "",
    fail: "",
    moneyCount: "",
    backgroundOne: "",
    backgroundTwo: "",
    backgroundThree: "",
    Dadisplay: false,
    dengdaiDisplay: false,
    dakaDisplay: false,
    nameOne: "",
    nameTwo: "",
    nameThree: "",
    infoName: {},
    userNameOne: "",
    userNameTwo: "",
    userNameThree: "",
    OnefontColor: "#fff",
    TwofontColor: "",
    ThreefontColor: "",
    FourfontColor: "",
    geth: "",
    getm: "",
    gets: "",
    timesTamp: ""
  },
  skip() {
    wx.navigateTo({
      url: "/pages/punchIncome/punchIncome?uid=" + this.data.uid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  isPay() {
    this.setData({
      top: "146rpx",
      display: "block",
      checked: true,
      value: "0"
    });
  },
  hidePay() {
    this.setData({
      top: "1207rpx",
      display: "none",
    });
  },
  // 选择购买的数量
  handlerOne() {
    this.setData({
      shoppCount: "1",
      imgUrlOne: "url('https://image.whwangdoudou.cn/extra/activity_btn.png')",
      imgUrlTwo: "",
      imgUrlThree: "",
      imgUrlFour: "",
      OnefontColor: "#fff",
      TwofontColor: "",
      ThreefontColor: "",
      FourfontColor: ""
    })
  },
  // 字符串过滤器
  filter(str) {
    console.log(str);
    let strs = "";
    if (str.length > 4) {
      strs = str.slice(0, 3) + "...";
    } else {
      strs = str;
    }
    return strs;
  },
  handlerTwo() {
    this.setData({
      shoppCount: "2",
      imgUrlOne: "",
      imgUrlTwo: "url('https://image.whwangdoudou.cn/extra/activity_btn.png')",
      imgUrlThree: "",
      imgUrlFour: "",
      OnefontColor: "",
      TwofontColor: "#fff",
      ThreefontColor: "",
      FourfontColor: ""
    })
  },
  handlerThree() {
    this.setData({
      shoppCount: "5",
      imgUrlOne: "",
      imgUrlTwo: "",
      imgUrlThree: "url('https://image.whwangdoudou.cn/extra/activity_btn.png')",
      imgUrlFour: "",
      OnefontColor: "",
      TwofontColor: "",
      ThreefontColor: "#fff",
      FourfontColor: ""
    })
  },
  handlerFour() {
    this.setData({
      shoppCount: "10",
      imgUrlOne: "",
      imgUrlTwo: "",
      imgUrlThree: "",
      imgUrlFour: "url('https://image.whwangdoudou.cn/extra/activity_btn.png')",
      OnefontColor: "",
      TwofontColor: "",
      ThreefontColor: "",
      FourfontColor: "#fff"
    })
  },
  // 选择支付方式
  onChange() {
    if (this.data.radio == "1") {
      this.setData({
        radio: "2"
      })
    } else {
      this.setData({
        radio: "1"
      })
    }
    console.log(this.data.radio);
  },
  onClick() {
    console.log(111);
  },
  // 点击确定支付--支付功能
  confirm() {
    let that = this;
    // console.log(this.data.radio);
    // return false;
    let data = {}
    let payUrlAcitve = utils.domain + "/activity.php";
    if (this.data.radio == "1") {
      payUrlAcitve = utils.domain + '/activity.php'
      data = {
        uid: this.data.uid,
        partake_count: this.data.shoppCount,
        m: "remained_pay",
        jump_weapp: utils.appid
      }
    } else {
      payUrlAcitve = utils.domain + '/web/lib/sign_pays/pay.php'
      data = {
        uid: this.data.uid,
        partake_count: this.data.shoppCount,
        jump_weapp: utils.appid
      }
    }
    // 支付
    post(data, payUrlAcitve).then(res => {
      // console.log(res)
      let payUrl = utils.domain + "/activity.php";
      let paySuccess = {
        m: "ifpay",
        out_trade_no: res.out_trade_no
      }
      console.log(payUrl, paySuccess)
      if (this.data.radio == "1") {
        if (res.r == "0") {
          this.setData({
            Dadisplay: false,
            dengdaiDisplay: true,
            dakaDisplay: false
          });
          // urlPunch = utils.domain+'/activity.php';
          Toast('支付成功');
          this.setData({
            top: "1207rpx",
            display: "none",
          });
        }
        if (res.r == "-1") {
          Toast('余额不足');
        }
        this.time();
      }

      if (this.data.radio == "2") {
        console.log(111)
        // let date=new Data();
        // localStorage.setItem("DaTime",date.parse(date.));
        wx.requestPayment({
          timeStamp:  res.pay_info.timeStamp,
          nonceStr:  res.pay_info.nonceStr,
          package:  res.pay_info.package,
          signType:  res.pay_info.signType,
          paySign: res.pay_info.paySign,
          success: function (result) {
            post(paySuccess, payUrl).then(res => {
              console.log(res)
              console.log("微信")
              if (res.pay == "1") {
                this.time();
                Toast('支付成功');
                console.log(this)
                that.hidePay();
                console.log("成功了吗")
                that.setData({
                  Dadisplay: false,
                  dengdaiDisplay: true,
                  dakaDisplay: false
                });
              }
            })
          },
          fail: function (res) {
            console.log(res)
          },
          complete: function (res) {
            console.log(res)
          }
        })
      }
    })
  },
  // 获取用户距离打卡时间
  time() {
    let that = this;
    setInterval(function () {
      var date = new Date();
      console.log(that.data.timesTamp);
      var remain = that.data.timesTamp - parseInt(date.getTime() / 1000);
      console.log(remain)
      var h = parseInt(remain / 60 / 60 % 24);
      var m = parseInt(remain / 60 % 60);
      var s = parseInt(remain % 60)
      console.log(h + "时" + m + "分" + s + "秒");
      that.setData({
        geth: h,
        getm: m,
        gets: s
      })
      // var m=parseInt(remain/60%60); 
      // var s=parseInt(remain%60);
      //   console.log(h+"时"+m+"分"+s+"秒");
      // console.log(date.getTime()/1000);
    }, 1000);
  },
  // 用户点击打卡
  isSuccess() {
    let data = {
      m: "daka",
      uid: this.data.uid
    }
    post(data, urlPunch).then(res => {
      console.log(res);
      if (res.r == "0") {
        this.setData({
          Dadisplay: true,
          dengdaiDisplay: false,
          dakaDisplay: false
        });
        Toast('打卡成功');
      }
    })
  },
  // 获取用户可以瓜分的金额
  people() {
    let data = {
      m: "today_join"
    }
    post(data, urlPunch).then(res => {
      this.setData({
        moneyCount: res.total
      });
    })
  },

  // 获取用户状态信息
  getstatus() {
    this.setData({
      uid: wx.getStorageSync("z_uid")
    });
    let data = {
      m: "is_activity",
      uid: this.data.uid
    }
    console.log(data);
    // 获取余额
    post(data, urlPunch).then(res => {
      console.log(res);
      if(typeof(res.activity_date)!="undefined"){
          this.setData({
            timesTamp: res.activity_date
        });
      }
      // let timesTamp=res.activity_date;
      
      if (res.z_check == "0") {
        wx.navigateTo({
          url: "/pages/auth/auth?uid=" + wx.getStorageSync("m_uid")
        })
      }
      if (res.status == "0") {
        this.setData({
          Dadisplay: true,
          dengdaiDisplay: false,
          dakaDisplay: false,
        })
      }
      if (res.status == "1") {
        this.setData({
          Dadisplay: false,
          dengdaiDisplay: true,
          dakaDisplay: false,
        })
        this.time();
      }

      if (res.status == "2") {
        this.setData({
          Dadisplay: false,
          dengdaiDisplay: false,
          dakaDisplay: true,
        })
      }
      if (res.status == "-1") {
        this.setData({
          Dadisplay: true,
          dengdaiDisplay: false,
          dakaDisplay: false,
        })
      }
      this.setData({
        balance: res.remained_money
      });
      // this.data.balance =
      function handler(e) {
        if (e._isScroller) return;
        e.preventDefault();
      }
    });
  },
  // 获取打卡详情信息
  getInfo() {
    let data = {
      m: "today_price"
    }
    post(data, urlPunch).then(res => {
      console.log(res);
      // console.log(res.lucky.username.slice(0,3));
      // console.log(res.lucky.username.length)
      // console.log(this.filter(res.lucky.username));
      res.lucky.username = this.filter(res.lucky.username);
      res.morning.username = this.filter(res.morning.username);
      res.lucky.username = this.filter(res.effort.username);
      console.log(res.lucky.username)
      this.setData({
        fail: res.fail,
        success: res.success,
        // moneyCount:res.lucky.money,
        backgroundOne: res.lucky.picurl,
        backgroundTwo: res.morning.picurl,
        backgroundThree: res.effort.picurl,
        infoName: res,
        userNameOne: res.lucky.username,
        userNameTwo: res.morning.username,
        userNameThree: res.lucky.username
        
      });
    })
  },
  onClick() { },
  onLoad: function (options) {
    // 获取uid
    console.log("打卡onLoad");
    // this.isSuccess();
    // this.time()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("打卡onReady");
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    console.log("打卡onShow");
    // 获取用户状态信息
    this.getstatus();
    // 获取打卡详情信息
    this.getInfo();
    // 获取用户可以瓜分的金额
    this.people();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  /*onShareAppMessage: function () {

  }*/
})